home *** CD-ROM | disk | FTP | other *** search
/ SunSoft Catalyst CDWARE 1996 May to August / Catalyst CDWARE 1996 May to August.iso / .products / SunSoft.CafeDelSol / JAVA / Wordsearch1.java < prev   
Text File  |  1996-01-19  |  11KB  |  399 lines

  1. /* Wordsearch, Copyright 1996, Eric C Harshbarger.
  2. This code is available for public use and modification as long as this notice of copyright is kept in tact. Additional credit is allowable for modifications.
  3.  
  4. Documentation on this applet may be found at:
  5. http://www.auburn.edu/~harshec/WWW/slideshow.html
  6.  
  7. Questions or comments should be directed to:
  8.  
  9. Eric Harshbarger ( http://www.auburn.edu/~harshec/ )
  10. harshec@mail.auburn.edu   OR    harshec@cdware.eng.sun.com   */
  11.  
  12. import java.applet.*;
  13. import java.awt.*;
  14. import java.util.StringTokenizer;
  15. import java.util.Vector;
  16. import java.lang.Math;
  17. import java.lang.String;
  18. import java.util.Hashtable;
  19. import java.net.URL;
  20. import sun.net.*;
  21. import java.awt.image.*;
  22. import java.net.MalformedURLException;
  23.  
  24. public class Wordsearch1 extends Applet implements Runnable {
  25.     
  26.     Image bufimage;
  27.     Graphics bufgraphic;
  28.     Dimension roo;
  29.     int gridsize;
  30.     int squaresize = 22;
  31.     int loopsize = 16;
  32.     int fontsize = 13;
  33.     int drawnloop[][];
  34.     int number = 0;
  35.     int tmploopx1,tmploopx2;
  36.     int tmploopy1,tmploopy2;
  37.     int attempts = 0;
  38.     int px,py,dx,dy;
  39.     int n,ne,e,se,s,sw,w,nw;
  40.     int loopcount = 0;
  41.     String wordlist;
  42.     String words[];
  43.     String placedwords[];
  44.     boolean foundwords[];
  45.     boolean loopstart = false;
  46.     boolean soundoff =true;
  47.     boolean blah = false;
  48.     String entry[][];
  49.     String title;
  50.     AudioClip noword;
  51.     AudioClip win;
  52.  
  53.  
  54.     public void run() {
  55.         repaint();
  56.     }
  57.  
  58.     public void init() {
  59.         String param;
  60.         StringTokenizer st;
  61.  
  62.         n=ne=e=se=s=sw=w=nw=0;
  63.         try {
  64.             noword = getAudioClip(new URL("http://www.auburn.edu/~harshec/WWW/audio/noword.au"));
  65.         } catch ( Exception e ) {}
  66.  
  67.         try {
  68.             win = getAudioClip(new URL("http://www.auburn.edu/~harshec/WWW/audio/win.au"));
  69.         } catch ( Exception e ) {}
  70.  
  71.         gridsize = (getParameter("GRIDSIZE") != null) ? Integer.parseInt(getParameter("GRIDSIZE")) : 10;
  72.         entry = new String[gridsize][gridsize]; 
  73.  
  74.         this.resize(squaresize*gridsize+225,squaresize*gridsize);
  75.  
  76.         roo = this.size();
  77.         bufimage = createImage(squaresize*gridsize+225,squaresize*gridsize);
  78.         bufgraphic = bufimage.getGraphics();
  79.  
  80.         wordlist = getParameter("WORDLIST");
  81.         st = new StringTokenizer(wordlist);
  82.         title = (getParameter("LISTNAME") != null) ? getParameter("LISTNAME") : "WORD LIST";
  83.  
  84.         number = st.countTokens();
  85.         words = new String[number];
  86.         placedwords = new String[number];
  87.         foundwords = new boolean[number];
  88.  
  89.         for (int i=0;i<number;i++) {
  90.             foundwords[i] = false;
  91.         }
  92.  
  93.         for (int i=0;i<number;i++) {
  94.             words[i] = st.nextToken();
  95.         }
  96.  
  97.         drawnloop = new int[number][4];
  98.  
  99.         placeWords();
  100.  
  101.         for (int i=0;i<gridsize;i++) {
  102.             for (int j=0;j<gridsize;j++) {
  103.  
  104.         char r = (char)(int)(26*Math.random()+65);
  105.         entry[i][j] = (entry[i][j] == null) ? String.valueOf(r) : entry[i][j];
  106.             }
  107.         }
  108.  
  109.     }
  110.  
  111.     public void placeWords() {
  112.         int i=0;
  113.         while (i<number && attempts < 5000) {
  114.             attempts ++;
  115.             double total = n+ne+e+se+s+sw+w+nw+1;
  116.             switch ((int)(1+Math.random()*8)) {
  117.                 default : { dx=dy=px=py=0; break; }
  118.                 case 1 : {
  119.                     if (n<total/6) {
  120.                     dx=0;dy=-1;
  121.                     px = (int)(Math.random()*gridsize);
  122.                     py = (int)(Math.random()*(gridsize-words[i].length()))+words[i].length();
  123.                     if (putWord(i,dx,dy,px,py)) { n++;i++; }
  124.                     break;
  125.                     }
  126.                 }
  127.                 case 2 : {
  128.                     if (ne<total/6) {
  129.                     dx=1;dy=-1;
  130.                     px = (int)(Math.random()*(gridsize-words[i].length()));
  131.                     py = (int)(Math.random()*(gridsize-words[i].length()))+words[i].length();
  132.                     if (putWord(i,dx,dy,px,py)) { ne++;i++; }
  133.                     break;
  134.                     }
  135.                 }
  136.                 case 3 : {
  137.                     if (e<total/6) {
  138.                     dx=1;dy=0;
  139.                     px = (int)(Math.random()*(gridsize-words[i].length()));
  140.                     py = (int)(Math.random()*gridsize);
  141.                     if (putWord(i,dx,dy,px,py)) { e++;i++; }
  142.                     break;
  143.                     }
  144.                 }
  145.                 case 4 : {
  146.                     if (se<total/6) {
  147.                     dx=1;dy=1;
  148.                     px = (int)(Math.random()*(gridsize-words[i].length()));
  149.                     py = (int)(Math.random()*(gridsize-words[i].length()));
  150.                     if (putWord(i,dx,dy,px,py)) { se++;i++; }
  151.                     break;
  152.                     }
  153.                 }
  154.                 case 5 : {
  155.                     if (s<total/6) {
  156.                     dx=0;dy=1;
  157.                     px = (int)(Math.random()*gridsize);
  158.                     py = (int)(Math.random()*(gridsize-words[i].length()));
  159.                     if (putWord(i,dx,dy,px,py)) { s++;i++; }
  160.                     break;
  161.                     }
  162.                 }
  163.                 case 6 : {
  164.                     if (sw<total/6) {
  165.                     dx=-1;dy=1;
  166.                     px = (int)(Math.random()*(gridsize-words[i].length()))+words[i].length();
  167.                     py = (int)(Math.random()*(gridsize-words[i].length()));
  168.                     if (putWord(i,dx,dy,px,py)) { sw++;i++; }
  169.                     break;
  170.                     }
  171.                 }
  172.                 case 7 : {
  173.                     if (w<total/6) {
  174.                     dx=-1;dy=0;
  175.                     px = (int)(Math.random()*(gridsize-words[i].length()))+words[i].length();
  176.                     py = (int)(Math.random()*gridsize);
  177.                     if (putWord(i,dx,dy,px,py)) { w++;i++; }
  178.                     break;
  179.                     }
  180.                 }
  181.                 case 8 : {
  182.                     if (nw<total/6) {
  183.                     dx=-1;dy=-1;
  184.                     px = (int)(Math.random()*(gridsize-words[i].length()))+words[i].length();
  185.                     py = (int)(Math.random()*(gridsize-words[i].length()))+words[i].length();
  186.                     if (putWord(i,dx,dy,px,py)) { nw++;i++; }
  187.                     break;
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.     }
  193.  
  194.     public boolean putWord(int i, int dx, int dy, int px, int py) {
  195.         String tempentry[] = new String[words[i].length()];
  196.         for (int j=0;j<words[i].length();j++) {
  197. //System.out.println(words[i]+","+j+" looking at spot:"+px+","+py+" with direction:"+ dx+","+dy);
  198.             String hopeful = words[i].substring(j,j+1);
  199.             if (entry[px+j*dx][py+j*dy] == null || entry[px+j*dx][py+j*dy].equals(hopeful.toUpperCase())) {
  200.                 tempentry[j] = hopeful.toUpperCase();
  201.  
  202.                 if (j == words[i].length()-1) {
  203.                     for (int k=0;k<words[i].length();k++) {
  204.                         entry[px+k*dx][py+k*dy] = tempentry[k];
  205.                     }
  206.                     placedwords[i] = words[i];
  207.                     return true;
  208.                 }
  209.             }
  210.             else return false;
  211.         }
  212.         return false;
  213.     }
  214.  
  215.     public void update(Graphics g) {
  216.         paint(g);
  217.     }
  218.  
  219.     public void paint(Graphics goo) {
  220.         paintBuffer(bufgraphic);
  221.         goo.drawImage(bufimage,0,0,this);
  222.     }
  223.  
  224.     public void paintBuffer(Graphics g) {
  225.  
  226.         g.clearRect(0,0,squaresize*gridsize+225,squaresize*gridsize);
  227.  
  228.         g.setColor(Color.lightGray);
  229.         g.fillRect(0,0,squaresize*gridsize+225,squaresize*gridsize);
  230.         g.setColor(new Color(110,110,110));
  231.         g.drawLine(0,0,0,squaresize*gridsize-1);
  232.         g.drawLine(0,0,squaresize*gridsize-1,0);
  233.         g.setColor(new Color(230,230,230));
  234.         g.drawLine(0,squaresize*gridsize-1,squaresize*gridsize-1,squaresize*gridsize-1);
  235.         g.drawLine(squaresize*gridsize-1,0,squaresize*gridsize-1,squaresize*gridsize-1);
  236.  
  237.         g.setColor(Color.black);
  238.  
  239.         g.setFont(new Font("courier",0,fontsize));
  240.         for (int i=0;i<gridsize;i++) {
  241.             for (int j=0;j<gridsize;j++) {
  242.         g.drawString(String.valueOf(entry[i][j]),i*squaresize+squaresize/2-3,j*squaresize+squaresize/2+5);
  243.             }
  244.         }
  245.  
  246.         int total = 0;
  247.         g.setFont(new Font("courier",0,fontsize));
  248.         g.setColor(Color.blue);
  249.         for (int i=0;i<placedwords.length;i++) {
  250.             if (placedwords[i] != null && !foundwords[i]) { g.drawString(placedwords[i],squaresize*gridsize+15,(squaresize-8)*total+30); total++; }
  251.         }
  252.  
  253.         g.setFont(new Font("courier",1,fontsize));
  254.         g.setColor(new Color(170,50,50));
  255.         g.drawString(title+" ("+String.valueOf(total)+")",squaresize*gridsize+15,15);
  256.  
  257.         g.setColor(Color.yellow);
  258.  
  259.         if (loopstart && loopcount<placedwords.length) {
  260.             int x1 = tmploopx1;
  261.             int y1 = tmploopy1;
  262.             int x2 = tmploopx2;
  263.             int y2 = tmploopy2;
  264.  
  265.             drawLoop (g,x1,y1,x2,y2);
  266.  
  267.         }
  268.         g.setColor(Color.black);
  269.  
  270.         for (int i=0;i<loopcount;i++) {
  271.             drawLoop (g,drawnloop[i][0],drawnloop[i][1],drawnloop[i][2],drawnloop[i][3]);
  272.         }
  273.  
  274.  
  275.     }
  276.  
  277.     public void drawLoop(Graphics g, int x1, int y1, int x2, int y2) {
  278.             int angle = calculateAngle(x1,y1,x2,y2);
  279.             int angle90 = (angle+90)%360;
  280.             int angle270 = (angle+270)%360;
  281.         
  282.             double rangle = angle*Math.PI/180;
  283.             double rangle90 = angle90*Math.PI/180;
  284.             double rangle270 = angle270*Math.PI/180;
  285.  
  286.             int nx1 = (int)(x1+squaresize/2+Math.cos(rangle90)*loopsize/2);
  287.             int ny1 = (int)(y1+squaresize/2-Math.sin(rangle90)*loopsize/2);
  288.             int nx2 = (int)(x2+squaresize/2+Math.cos(rangle270)*loopsize/2);
  289.             int ny2 = (int)(y2+squaresize/2-Math.sin(rangle270)*loopsize/2);
  290.  
  291.             int nx3 = (int)(x1+squaresize/2+Math.cos(rangle270)*loopsize/2);
  292.             int ny3 = (int)(y1+squaresize/2-Math.sin(rangle270)*loopsize/2);
  293.             int nx4 = (int)(x2+squaresize/2+Math.cos(rangle90)*loopsize/2);
  294.             int ny4 = (int)(y2+squaresize/2-Math.sin(rangle90)*loopsize/2);
  295.  
  296.             g.drawArc(x1+(squaresize-loopsize)/2,y1+(squaresize-loopsize)/2,loopsize,loopsize,(angle90),180);
  297.             g.drawArc(x2+(squaresize-loopsize)/2,y2+(squaresize-loopsize)/2,loopsize,loopsize,(angle270),180);
  298.             
  299.             g.drawLine(nx1,ny1,nx4,ny4);
  300.             g.drawLine(nx3,ny3,nx2,ny2);
  301.     }
  302.  
  303.     public int calculateAngle(int x1, int y1, int x2, int y2) {
  304.             int angle = (int)(Math.atan((double)(y2-y1)/(x1-x2))*180/Math.PI);
  305.  
  306.             if (x2 > x1) {
  307.                 if (y1 == y2) { angle = 0; }
  308.                 else if (y2 > y1) { angle = angle+360;}
  309.             }
  310.             else { angle = angle+180; }
  311.             return angle;
  312.     }
  313.  
  314.     public boolean mouseDown(Event evt, int mx, int my) {
  315.  
  316.         if (mx < squaresize*gridsize && mx > 0 && my < squaresize*gridsize && my > 0) {
  317.         loopstart = true;
  318.  
  319.         tmploopx1 = squaresize*(int)(mx/squaresize);
  320.         tmploopy1 = squaresize*(int)(my/squaresize);
  321.         tmploopx2 = squaresize*(int)(mx/squaresize);
  322.         tmploopy2 = squaresize*(int)(my/squaresize);
  323.         repaint();
  324.         }
  325.  
  326.         return true;
  327.     }
  328.  
  329.     public boolean mouseDrag(Event evt, int mx, int my) {
  330.  
  331.         if (mx < squaresize*gridsize && mx > 0 && my < squaresize*gridsize && my > 0) {
  332.  
  333.         tmploopx2 = squaresize*(int)(mx/squaresize);
  334.         tmploopy2 = squaresize*(int)(my/squaresize);
  335.         repaint();
  336.         }
  337.  
  338.         return true;
  339.     }
  340.  
  341.     public boolean mouseUp(Event evt, int mx, int my) {
  342.  
  343.         int bx = tmploopx1/squaresize;
  344.         int by = tmploopy1/squaresize;
  345.         int ex = tmploopx2/squaresize;
  346.         int ey = tmploopy2/squaresize;
  347.  
  348.         if (bx == ex || by == ey || Math.abs(bx-ex) == Math.abs(by-ey)) {
  349.  
  350.             String test = "!";
  351.             String testrev = "!";
  352.             int ddx,ddy;
  353.  
  354.             if (bx == ex) ddx = 0;
  355.             else if (bx > ex) ddx = -1;
  356.             else ddx = 1;
  357.  
  358.             if (by == ey) ddy = 0;
  359.             else if (by > ey) ddy = -1;
  360.             else ddy = 1;
  361.  
  362.             for (int i=0;i<Math.max(Math.abs(bx-ex),Math.abs(by-ey))+1;i++) {
  363.                 test = test.concat(entry[bx+i*ddx][by+i*ddy]);
  364.             }
  365.  
  366.             for (int i=0;i<Math.max(Math.abs(bx-ex),Math.abs(by-ey))+1;i++) {
  367.                 testrev = testrev.concat(entry[ex-i*ddx][ey-i*ddy]);
  368.             }
  369.  
  370.  
  371.             for (int i=0;i<words.length;i++) {
  372.                 if (words[i].toUpperCase().equals(test.substring(1)) || words[i].toUpperCase().equals(testrev.substring(1))) {
  373.                     drawnloop[loopcount][0] = tmploopx1;
  374.                     drawnloop[loopcount][1] = tmploopy1;
  375.                     drawnloop[loopcount][2] = tmploopx2;
  376.                     drawnloop[loopcount][3] = tmploopy2;
  377.  
  378.                     soundoff = false;
  379.                     foundwords[i] = true;
  380.                     loopcount++;
  381.                     repaint();
  382.                     if (loopcount == placedwords.length) playFinale();
  383.                 }
  384.             }
  385.         }
  386.         if (soundoff) noword.play();
  387.         soundoff =true;
  388.         return true;
  389.     }
  390.  
  391.     public void playFinale() {
  392.         win.play();
  393.         try {Thread.sleep(5000);} catch (InterruptedException e){};
  394.         loopcount = 0;
  395.         init();
  396.     }
  397.  
  398. }
  399.